﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="RepresentativeOrganisationData", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(RepresentativeOrganisationData))]
    public class RepresentativeOrganisationDataSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IRepresentativeOrganisationData clazz;
        
        public RepresentativeOrganisationDataSerializer(IRepresentativeOrganisationData clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("RepresentativeOrganisation");
            if (clazz.RepresentativeOrganisation!=null) {
                global::Xtee.Core.SerializationMetadata metaRepresentativeOrganisation = this.GetMetaData(clazz.RepresentativeOrganisation);
                Writer.WriteTypeAttribute(metaRepresentativeOrganisation.TypeQualifiedName.Namespace, metaRepresentativeOrganisation.TypeQualifiedName.Name);
                metaRepresentativeOrganisation.NewSerializer(clazz.RepresentativeOrganisation).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : RepresentativeOrganisation");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("Type");
            if (clazz.Type!=null) {
                Writer.WriteTypeAttribute("http://producers.kis.xtee.riik.ee/producer/kis", "RepresentativeType");
                Writer.WriteString(clazz.Type.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Type");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("IsSender");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "boolean");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdBoolean(((bool)(clazz.IsSender))).ToString());
            Writer.WriteEndElement();
            for (int x = 0; ((clazz.ContactInformation != null) 
                        && (x < clazz.ContactInformation.Count)); x = (x + 1)) {
                if (clazz.ContactInformation[x]!=null) {
                    Writer.WriteStartElement("ContactInformation");
                    global::Xtee.Core.SerializationMetadata metaContactInformation = this.GetMetaData(clazz.ContactInformation[x]);
                    Writer.WriteTypeAttribute(metaContactInformation.TypeQualifiedName.Namespace, metaContactInformation.TypeQualifiedName.Name);
                    metaContactInformation.NewSerializer(clazz.ContactInformation[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            Writer.WriteStartElement("Address");
            if (clazz.Address!=null) {
                global::Xtee.Core.SerializationMetadata metaAddress = this.GetMetaData(clazz.Address);
                Writer.WriteTypeAttribute(metaAddress.TypeQualifiedName.Namespace, metaAddress.TypeQualifiedName.Name);
                metaAddress.NewSerializer(clazz.Address).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Address");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="RepresentativeOrganisation") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeOrganisationData>RepresentativeOrganisation");
                }
                global::Xtee.Core.SerializationMetadata metaRepresentativeOrganisation = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IOrganisation>(Reader, "Organisation", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.RepresentativeOrganisation = metaRepresentativeOrganisation.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IOrganisation>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("RepresentativeOrganisation");
                    metaRepresentativeOrganisation.NewSerializer(clazz.RepresentativeOrganisation).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeOrganisationData>RepresentativeOrganisation", Reader.LocalName);
            }
            if (Reader.LocalName=="Type") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeOrganisationData>Type");
                }
                clazz.Type = new global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.RepresentativeType(Reader.ReadElementString("Type"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeOrganisationData>Type", Reader.LocalName);
            }
            if (Reader.LocalName=="IsSender") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeOrganisationData>IsSender");
                }
                clazz.IsSender = new global::Xtee.Core.Schema.XsdBoolean(Reader.ReadElementString("IsSender")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeOrganisationData>IsSender", Reader.LocalName);
            }
            if (Reader.LocalName=="ContactInformation") {
                clazz.ContactInformation = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "ContactInformation")); ) {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeOrganisationData>ContactInformation");
                    }
                    global::Xtee.Core.SerializationMetadata metaContactInformation = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation>(Reader, "ContactInformation", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation item = metaContactInformation.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation>();
                    clazz.ContactInformation.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("ContactInformation");
                        metaContactInformation.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="Address") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeOrganisationData>Address");
                }
                global::Xtee.Core.SerializationMetadata metaAddress = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IAddress>(Reader, "Address", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.Address = metaAddress.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IAddress>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("Address");
                    metaAddress.NewSerializer(clazz.Address).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeOrganisationData>Address", Reader.LocalName);
            }
        }
    }
}
